package com.solution.pc.my.controller.finance;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.finance.wallet.entity.WalletFlowEntity;
import com.solution.admin.finance.wallet.service.WalletFlowService;
import com.solution.common.controller.BaseController;
import com.solution.common.page.JSONGrid;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.utils.string.StringUtil;

/**
 * 客户钱包流水web控制层
 * @author lufer
 * @date 2018-05-04 14:19:15
 */
@RequestMapping("pc/my/pcWalletFlow")
@Controller
public class WalletFlowController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(WalletFlowController.class);

	@Autowired
	private WalletFlowService walletFlowService;
	
	/**
	 * 跳转客户钱包流水查询页面
	 * @param WalletFlow 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author lufer
 	 * @date 2018-05-04 14:19:15
	 */
	@RequestMapping("/toListWalletFlowPage")
	public String toListWalletFlowPage(WalletFlowEntity walletFlow, HttpServletRequest request) {
		return "walletFlow/walletFlow_list";
	}

	/**
	 * 分页查询客户钱包流水列表
	 * @param walletFlow 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author lufer
 	 * @date 2018-05-04 14:19:15
	 */
	@RequestMapping("/listWalletFlowPage")
	@ResponseBody
	public JSONGrid listWalletFlowPage(WalletFlowEntity walletFlow, HttpServletRequest request){
		int pageNum=Integer.parseInt(request.getParameter("page").toString());
		int numPerPage=Integer.parseInt(request.getParameter("rows").toString());
		PageParam pageParam = super.getPageParam(request);
		pageParam.setPageNum(pageNum);
		pageParam.setNumPerPage(numPerPage);
		
		
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		try {
			PageBean pageBean = walletFlowService.listWalletFlowPage(pageParam, paramMap);
			return new JSONGrid(pageBean.getRecordList(),pageBean.getTotalCount());
		} catch (Exception e) {
			handleException("分页查询客户钱包流水失败", logger, e);
		}
		return new JSONGrid();
	}
	
	
	/**
	 * 跳转新增页面
	 * @return string 跳转页面路径
	 * @author lufer
 	 * @date 2018-05-04 14:19:15
	 */
	@RequestMapping("/toAddWalletFlow")
	public String toAddWalletFlow(HttpServletRequest request) {
		request.setAttribute("opFlag", "add");
		return "walletFlow/walletFlow_form";
	}

	/**
	 * 跳转编辑页面
	 * @return string 跳转页面路径
	 * @author lufer
 	 * @date 2018-05-04 14:19:15
	 */
	@RequestMapping("/toUpdateWalletFlow")
	public String toUpdateWalletFlow(HttpServletRequest request) {
		// 编辑操作
		request.setAttribute("opFlag", "edit");
		long id = Long.valueOf(request.getParameter("id"));
		try {
			WalletFlowEntity walletFlow = walletFlowService.getWalletFlowById(id);
			request.setAttribute("walletFlow", walletFlow);
		} catch (Exception e) {
			handleException("跳转编辑客户钱包流水页面错误", logger, e);
		}
		return "walletFlow/walletFlow_form";
	}


	/**
	 * 新增或保存客户钱包流水
	 * @param request 请求对象
	 * @param walletFlowEntity 客户钱包流水实体
	 * @return
	 * @author lufer
 	 * @date 2018-05-04 14:19:15
	 */
	@RequestMapping("/saveOrUpdateWalletFlow")
	@ResponseBody
	public Map<String, Object> saveWalletFlow(HttpServletRequest request, WalletFlowEntity walletFlowEntity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		try {
			String id = request.getParameter("id");
			if(StringUtil.isNotEmpty(id)){
				result = walletFlowService.updateWalletFlow(walletFlowEntity);
			}else{
				result = (int)walletFlowService.saveWalletFlow(walletFlowEntity);
			}
			resultMap.put("code", result);
		} catch (Exception e) {
			handleException("添加客户钱包流水失败", logger, e);
		}
		return resultMap;
	}
	
}
